﻿using ConsoleApp1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mainform
{
    public partial class Form1 : Form
    {
        private readonly OrderService _orderService = new OrderService();
        private readonly BindingSource _orderBindingSource = new BindingSource();
        private readonly BindingSource _detailBindingSource = new BindingSource();

        public Form1()
        {
            InitializeComponent();
            SetupDataBinding();
            LoadOrders();
            ConfigureGrids();
        }

        private void SetupDataBinding()
        {
            // 主订单绑定
            _orderBindingSource.DataSource = typeof(Order);
            dataGridViewOrders.DataSource = _orderBindingSource;

            // 明细绑定（通过 DataMember 关联）
            _detailBindingSource.DataSource = _orderBindingSource;
            _detailBindingSource.DataMember = "Details";
            dataGridViewDetails.DataSource = _detailBindingSource;

            // 文本框绑定
            txtOrderId.DataBindings.Add("Text", _orderBindingSource, "OrderId");
            txtCustomer.DataBindings.Add("Text", _orderBindingSource, "Customer.Name");
            txtTotal.DataBindings.Add("Text", _orderBindingSource, "TotalAmount", true, DataSourceUpdateMode.Never, "", "C");
        }

        private void LoadOrders()
        {
            _orderBindingSource.DataSource = _orderService.GetAllOrders();
        }

        private void ConfigureGrids()
        {
            // 订单列表列配置
            dataGridViewOrders.Columns.Clear();
            dataGridViewOrders.Columns.Add("OrderId", "订单号");
            dataGridViewOrders.Columns.Add("CustomerName", "客户");
            dataGridViewOrders.Columns.Add("OrderDate", "日期");

            // 添加总金额列并设置格式
            var amountColumn = new DataGridViewTextBoxColumn
            {
                Name = "TotalAmount",
                HeaderText = "总金额"
            };
            amountColumn.DefaultCellStyle.Format = "C";  // 货币格式
            dataGridViewOrders.Columns.Add(amountColumn);

            // 明细列表列配置  
            dataGridViewDetails.Columns.Clear();
            dataGridViewDetails.Columns.Add("ProductName", "商品");
            dataGridViewDetails.Columns.Add("Quantity", "数量");

            // 添加金额列并设置格式
            var detailAmountColumn = new DataGridViewTextBoxColumn
            {
                Name = "Amount",
                HeaderText = "金额"
            };
            detailAmountColumn.DefaultCellStyle.Format = "C";  // 货币格式
            dataGridViewDetails.Columns.Add(detailAmountColumn);
        }

        // 按钮事件
        private void btnSearch_Click(object sender, EventArgs e) { /* 搜索逻辑 */ }
        private void btnAdd_Click(object sender, EventArgs e) { /* 打开添加窗口 */ }
        private void btnEdit_Click(object sender, EventArgs e) { /* 打开编辑窗口 */ }
        private void btnDelete_Click(object sender, EventArgs e) { /* 删除订单 */ }
        private void txtKeyword_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
